<?php defined('DML') or die("Access locked");

class PageNavigation{
   private $totalrecords;  //количество записей
   private $page;          //текущая страница
   private $totalpages;    //общее колличество страниц
   private $limit;         //колличество записий на страницу
   private $url;           //адрес страницы
   private $link;          //ссылки на страницы


   public function __construct($page, $totalrecords, $url,  $limit = 10) {
      $this->page = intval($page);
      $this->totalrecords = $totalrecords;
      $this->url = $url;
      $this->limit = $limit;
      
      if($this->totalrecords > $this->limit){
         $this->TotalPages();
         $this->LinkPages();
         $this->PreviousAndNext();
         $this->FirstAndLast();
      }
   }
   
   
   public function getNavigator(){
      return @$this->link['first'].@$this->link['prev'].$this->link['linkpage'].@$this->link['next'].@$this->link['last'];
   }
   
   private function LinkPages(){
      for($i=1; $i < $this->totalpages+1; $i++){
         if($i != $this->page){
            $str = "<a href=\"".$this->url."page=$i\">$i </a>";
         }else{
            $str = "$i ";
         }
         @$this->link['linkpage'] .= $str;
      }
   }
   
   private function PreviousAndNext(){
      $prev = $this->page - 1;
      if($this->page != 1){
         $this->link['prev'] = "<a href=\"".$this->url."page=$prev\">Prev </a>";
      }
      
      $next = $this->page + 1;
      if($this->page != $this->totalpages){
         $this->link['next'] = "<a href=\"".$this->url."page=$next\"> Next</a>";
      }      
   }
   
   private function FirstAndLast(){
      $first = 1;
      if($first != $this->page){
         $this->link['first'] = "<a href=\"".$this->url."page=$first\"><<< </a>";
      }
      
      $last = $this->totalpages;
      if($last != $this->page){
         $this->link['last'] = "<a href=\"".$this->url."page=$last\"> >>></a>";
      }
   }

   private function TotalPages(){
      $this->totalpages = ceil($this->totalrecords/$this->limit);
   }
   
}
?>